/**
 * 
 */
package hottargui.config;

import hottargui.framework.Game;
import hottargui.framework.GameData;
import hottargui.framework.Player;
import hottargui.framework.PlayerColor;
import hottargui.framework.WinnerStrategy;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TreasuryWinnerStrategy implements WinnerStrategy {

	class TreasuryComparator implements Comparator<Player> {

		public int compare(Player p1, Player p2) {
			return ((Integer) p1.getCoins()).compareTo(p2.getCoins());
		}
	}

	public PlayerColor getWinner(Game game) {
		GameData gd = game.getGameObjectServer().getGameData();
		List<Player> treasuryRanking = (List<Player>) gd.getPlayers().values();
		Collections.sort(treasuryRanking, new TreasuryComparator());
		/*
		 * if the 2 players with most treasury collected after last round is
		 * identical then it is considered a draw
		 */
		if (treasuryRanking.get(0).getCoins() != treasuryRanking.get(1).getCoins()) {
			return treasuryRanking.get(0).getColor();
		}
		return PlayerColor.None;
	}
}
